import { RootThunkAction } from "..";
import request from '@/utils/request'
import { ArticelAction, Comment } from "../reducers/article";

export function getArticleDetail(id :string): RootThunkAction {
    return async (dispatch) => {
        const res= await request.get('/articles/' + id)
        dispatch({
            type: 'artcile/saveDetail',
            payload: res.data
        })
    }
}

// 获取文章的评论
export function getCommentList(id: string): RootThunkAction {
    return async (dispatch) => {
        const res = await request.get('/comments', {
            params: {
                type: 'a',
                source: id
            }
        })
        dispatch({
            type: 'artcile/saveComment',
            payload: res.data
        })
    }
}

// 获取文章的评论
export function getMoreCommentList(id: string, offset: string): RootThunkAction {
    return async (dispatch) => {
        const res = await request.get('/comments', {
            params: {
                type: 'a',
                source: id,
                offset
            }
        })
        dispatch({
            type: 'artcile/saveMoreComment',
            payload: res.data
        })
    }
}

// 点赞
export function likeAritcle(id: string, attitude: number): RootThunkAction {
    return async (dispatch) => {
        if (attitude === 1) {
            // 取消点赞
            await request.delete('/article/likings/' + id)
        } else {
            // 点赞
            await request.post('/article/likings', { target: id })
        }
        // 更新
        await dispatch(getArticleDetail(id))
    }
}

// 收藏
export function collectArticle(id: string, is_collected: boolean): RootThunkAction {
    return async (dispatch) => {
        if (is_collected) {
            // 取消收藏
            await request.delete('/article/collections/' + id)
        } else {
            // 收藏
            await request.post('article/collections', { target: id })
        }
        // 更新
        await dispatch(getArticleDetail(id))
    }
}

// 关注
export function followUser(userId: string, is_follow: boolean): RootThunkAction {
    return async (dispatch, getState) => {
        if (is_follow) {
            // 取消关注
            await request.delete('/user/followings/' + userId)
        } else {
            // 关注
            await request.post('/user/follwings', {
                target: userId
            })
        }
        await dispatch(getArticleDetail(getState().article.detail.art_id))
    }
}

// 收藏
export function addComment(articleId: string, content: string): RootThunkAction {
    return async (dispatch, getState) => {
        const res = await request.post('/comments', {
            target: articleId,
            content
        })
        dispatch({
            type: 'artcile/saveNewComment',
            payload: res.data.new_obj
        })
        dispatch(getArticleDetail(getState().article.detail.art_id))
    }
}

// 更新回复条数
export function updateComment(comment: Comment): ArticelAction {
    // 更新某一个评论消息
    return {
        type: 'artcile/updateComment',
        payload: comment
    }
}
